package cn.xiaoming.car.server.account.controller;

import cn.xiaoming.car.server.account.pojo.param.UserAddNewParam;
import cn.xiaoming.car.server.account.pojo.param.UserLoginInfoParam;
import cn.xiaoming.car.server.account.pojo.param.UserUpdataPasswordParam;
import cn.xiaoming.car.server.account.pojo.vo.AdminListItemVO;
import cn.xiaoming.car.server.account.pojo.vo.UserListItemVO;
import cn.xiaoming.car.server.account.pojo.vo.UserLoginResultVO;
import cn.xiaoming.car.server.account.pojo.vo.UserStandardVO;
import cn.xiaoming.car.server.account.service.IUserService;
import cn.xiaoming.car.server.common.pojo.vo.PageData;
import cn.xiaoming.car.server.common.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/account/user")
@Validated
@Api(tags = "1.2. 账号管理-用户管理")
public class UserController {

    @Autowired
    private IUserService userService;

    public UserController(){
        log.info("创建控制器对象：UserController");
    }

    @PostMapping("/login")
    @ApiOperation("用户登陆")
    @ApiOperationSupport(order = 10)
    public JsonResult login(@RequestBody UserLoginInfoParam userLoginInfoParam) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginInfoParam);
        UserLoginResultVO userLoginResultVO=userService.login(userLoginInfoParam);
        return JsonResult.ok(userLoginResultVO);
    }

    @PostMapping("/logout")
    @ApiOperation("用户退出登录")
    @ApiOperationSupport(order = 11)
    public JsonResult logout() {
        log.debug("开始处理【用户退出登录】的请求，无参数");

        return JsonResult.ok();
    }

    @PostMapping("/reg")
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Validated UserAddNewParam userAddNewParam) {
        log.debug("开始处理【用户注册】的请求，参数：{}", userAddNewParam);
        userService.addNew(userAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable Long id){
        log.debug("开始处理【根据ID删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/updata/password")
    @ApiOperation("根据ID修改用户密码")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult updataPassword(@PathVariable Long id, @RequestBody UserUpdataPasswordParam userUpdataPasswordParam){
        log.debug("开始处理【根据ID更改用户密码】的请求，参数：{}",id);
        userService.updataPassword(id,userUpdataPasswordParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/enable")
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/disable")
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }


    @ApiOperation("根据ID查询用户")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    @GetMapping("/{id:[0-9]+}")
    public JsonResult getStandardById(@PathVariable @Range(min = 1, message = "获取管理员详情失败，请提交合法的ID值！") Long id) {
        log.debug("开始处理【根据ID查询管理员】的请求，参数：{}", id);
        UserStandardVO user = userService.getStandardById(id);
        return JsonResult.ok(user);
    }

    @GetMapping("")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", dataType = "long")
    })
    public JsonResult list(Integer page) {
        log.debug("开始处理【查询用户列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<UserListItemVO> pageData = userService.list(pageNum);
        return JsonResult.ok(pageData);
    }
}
